<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input
        clearable
        placeholder="请输入内容"
        v-model="params.carNumber"
        class="search-main"
      />
      <span class="search-label">车主姓名：</span>
      <el-input
        v-model="params.personName"
        clearable
        placeholder="请输入内容"
        class="search-main"
      />
      <span class="search-label">状态：</span>
      <el-select v-model="params.cardStatus">
        <el-option
          v-for="item in selectStatus"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button type="primary" class="search-btn" @click="queryCardList">
        查询
      </el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button
        v-permission="'parking:card:add_edit'"
        type="primary"
        @click="$router.push('/parking/card/addMonthCard')"
      >
        添加月卡
      </el-button>
      <el-button v-permission="'parking:card:remove'" @click="batchDelMonthCard"
        >批量删除</el-button
      >
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table
        style="width: 100%"
        :data="list"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column type="index" label="序号" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column
          label="状态"
          prop="cardStatus"
          :formatter="formatterStatus"
        />

        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button
              size="mini"
              type="text"
              v-permission="'parking:card:recharge'"
            >
              续费
            </el-button>
            <el-button
              size="mini"
              type="text"
              v-permission="'parking:card:query'"
            >
              查看
            </el-button>
            <el-button
              size="mini"
              type="text"
              v-permission="'parking:card:add_edit'"
              @click="editCard(scope.row.id)"
            >
              编辑
            </el-button>
            <el-button
              v-permission="'parking:card:remove'"
              size="mini"
              type="text"
              @click="delMonthCard(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        :current-page="params.page"
        :page-sizes="[2, 4, 6, 10]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tatalPage"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 添加楼宇 -->
    <el-dialog title="添加楼宇" width="580px">
      <!-- 表单接口 -->
      <div class="form-container" />
      <template #footer>
        <el-button size="mini"> 取 消 </el-button>
        <el-button size="mini" type="primary"> 确 定 </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { deleteCardAPI, getCardListAPI } from '@/api/card'
export default {
  name: 'Card',
  data () {
    return {
      list: [],
      params: {
        page: 1,
        pageSize: 10,
        carNumber: '',
        personName: '',
        cardStatus: null // 知识点：axios会默认不传递值为空的参数
      },
      tatalPage: 0,
      selectStatus: [
        { value: null, label: '全部' },
        { value: 0, label: '可用' },
        { value: 1, label: '已过期' }
      ],
      selectionvals: []
    }
  },
  created () {
    this.getCardList()
  },
  methods: {
    // 批量删除月卡
    batchDelMonthCard () {
      if (this.selectionvals.length === 0) {
        return
      }
      const ids = this.selectionvals.map(item => item.id)
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示')
        .then(async () => {
          await deleteCardAPI(ids.join(','))
          this.$message({
            type: 'success',
            message: '批量删除成功!'
          })
          this.getCardList()
        })
        .catch(() => {})
    },
    handleSelectionChange (val) {
      this.selectionvals = val
    },
    //删除月卡
    delMonthCard (id) {
      //弹窗
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await deleteCardAPI(id)
          // 知识点：做分页列表【删除】的时候，都需要处理这种情况（当前页只有一条数据，删除了它要把页码-1）
          if (this.list.length === 1 && this.params.page > 1) {
            page--
          }
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getCardList()
        })
        .catch(() => {})
    },
    editCard (id) {
      // 知识点：路由传参，地址栏？拼接方式，可以用path属性，也可以用name属性（如果有）
      this.$router.push({
        path: '/parking/card/addMonthCard',
        query: {
          id
        }
      })
    },
    async getCardList () {
      const res = await getCardListAPI(this.params)
      this.list = res.data.rows
      this.tatalPage = res.data.total
    },
    // 格式化数据状态展示
    formatterStatus (row, column, cellValue, index) {
      // 知识点：多种状态需要展示，用这种写法避免大量的if判断
      const status = {
        0: '可用',
        1: '已过期'
      }
      return status[cellValue]
    },
    handleSizeChange (val) {
      this.params.pageSize = val
      this.getCardList()
    },
    handleCurrentChange (val) {
      this.params.page = val
      this.getCardList()
    },
    queryCardList () {
      // 知识点： 【分页查询】的时候必须从第一页开始，否则当在第二页的时候点查询，会导致没数据
      this.params.page = 1
      this.getCardList()
    }
  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn {
    margin-left: 20px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.form-container {
  padding: 0px 80px;
}
</style>
